package com.okay.word

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.okay.util.DensityUtils.dp2px

class WordView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    var mPaint = Paint()

    /**
     * 当前view的坐标
     */
    var leftLay = 0f
    var topLay = 0f
    var rightLay = 0f
    var bottomLay = 0f


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d("BarrageView", "onMeasure")
        setMeasuredDimension(
            measureWidth(widthMeasureSpec),
            measureHeight(heightMeasureSpec)
        );//设置宽高为wrap_content的时候 给一个默认值
    }

    fun measureWidth(measureSpec: Int): Int {
        var specMode = MeasureSpec.getMode(measureSpec)
        var specSize = MeasureSpec.getSize(measureSpec)
        var result = 200
        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize
        }
        return result;
    }

    fun measureHeight(measureSpec: Int): Int {
        var specMode = MeasureSpec.getMode(measureSpec);
        var specSize = MeasureSpec.getSize(measureSpec);
        var result = 200
        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize
        }
        return result
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        Log.d("BarrageView", "onLayout")
        Log.d("BarrageView", "left：$left  top：$top  right：$right  bottom：$bottom")

        leftLay = left.toFloat()
        topLay = top.toFloat()
        rightLay = right.toFloat()
        bottomLay = bottom.toFloat()

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        mPaint.isAntiAlias = true //设置是否抗锯齿;
        mPaint.style = Paint.Style.STROKE //设置填充样式 Paint.Style.FILL_AND_STROKE 填充且描边  Paint.Style.STROKE 描边   Paint.Style.FILL 填充
        mPaint.color = Color.parseColor("#FFCCCCCC");//设置画笔颜色
        mPaint.strokeWidth = 1F;//设置画笔宽度

        var rect = RectF(
            dp2px(context,40f).toFloat(),
            dp2px(context,30f).toFloat(),
            dp2px(context,40f+146f).toFloat(),
            dp2px(context,30f+36).toFloat()
        )
        canvas?.drawRoundRect(rect, 2f, 2f, mPaint)

        mPaint.style = Paint.Style.FILL //设置填充样式 Paint.Style.FILL_AND_STROKE 填充且描边  Paint.Style.STROKE 描边   Paint.Style.FILL 填充
        mPaint.color = Color.parseColor("#FF89AAFF");//设置画笔颜色
        var rect1 = RectF(
            dp2px(context,40f).toFloat(),
            dp2px(context,42f).toFloat(),
            dp2px(context,40f+4f).toFloat(),
            dp2px(context,42f+12).toFloat()
        )
        canvas?.drawRoundRect(rect1, 2f, 2f, mPaint)


        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.color = Color.parseColor("#FF444444");//设置画笔颜色
        mPaint.style = Paint.Style.FILL
        var fontMetrics = mPaint.fontMetrics
        var y1 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("单词听懂会说",
            dp2px(context,54f).toFloat(),dp2px(context,38f).toFloat()+ y1,
            mPaint
        )


        mPaint.textSize = dp2px(context,40f).toFloat()
        mPaint.color = Color.parseColor("#FF222222");//设置画笔颜色
        mPaint.style = Paint.Style.FILL
        var y2 = dp2px(context,40f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("wonder",
            dp2px(context,40f).toFloat(),dp2px(context,92f).toFloat()+ y2,
            mPaint
        )

        mPaint.textSize = dp2px(context,22f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y3 = dp2px(context,22f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("英  [ˈwʌndə(r)] ",
            dp2px(context,40f).toFloat(),dp2px(context,164f).toFloat()+ y3,
            mPaint
        )

        mPaint.textSize = dp2px(context,22f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y4 = dp2px(context,22f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("美 [ˈwʌndə(r)] ",
            dp2px(context,288f).toFloat(),dp2px(context,164f).toFloat()+ y4,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.color = Color.parseColor("#FF5C5C5C");//设置画笔颜色
        mPaint.style = Paint.Style.FILL
        var y5 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("v.",
            dp2px(context,40f).toFloat(),dp2px(context,220f).toFloat()+ y5,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y6 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("n.",
            dp2px(context,40f).toFloat(),dp2px(context,260f).toFloat()+ y6,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y7 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("adj.",
            dp2px(context,40f).toFloat(),dp2px(context,300f).toFloat()+ y7,
            mPaint
        )
//        想知道；想弄明白；琢磨；礼貌地提问或请人做事时说；感到诧异；非常惊讶
//        惊讶；惊奇；惊异；惊叹；奇迹；奇观；奇事；奇妙之处；能人；奇才；有特效的东西
//        极好的；极有效的；非常美丽的
        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.color = Color.parseColor("#FF222222");//设置画笔颜色
        mPaint.style = Paint.Style.FILL
        var y8 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("想知道；想弄明白；琢磨；礼貌地提问或请人做事时说；感到诧异；非常惊讶",
            dp2px(context,98f).toFloat(),dp2px(context,220f).toFloat()+ y8,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y9 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("惊讶；惊奇；惊异；惊叹；奇迹；奇观；奇事；奇妙之处；能人；奇才；有特效的东西",
            dp2px(context,98f).toFloat(),dp2px(context,260f).toFloat()+ y9,
            mPaint
        )
        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y10 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的极好的；极有效的；非常美丽的",
            dp2px(context,98f).toFloat(),dp2px(context,300f).toFloat()+ y10,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.style = Paint.Style.FILL
        var y11 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("第三人称单数：wonders          复数：wonders          现在分词：wondering          过去式：wondered",
            dp2px(context,40f).toFloat(),dp2px(context,352f).toFloat()+ y11,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.color = Color.parseColor("#FF757575");//设置画笔颜色
        mPaint.style = Paint.Style.FILL
        var y12 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("[例句] ",
            dp2px(context,40f).toFloat(),dp2px(context,448f).toFloat()+ y12,
            mPaint
        )
        canvas?.drawText("[例句] ",
            dp2px(context,40f).toFloat(),dp2px(context,508f).toFloat()+ y12,
            mPaint
        )
        canvas?.drawText("[例句] ",
            dp2px(context,40f).toFloat(),dp2px(context,568f).toFloat()+ y12,
            mPaint
        )

        mPaint.textSize = dp2px(context,20f).toFloat()
        mPaint.color = Color.parseColor("#FF222222");//设置画笔颜色
        mPaint.style = Paint.Style.FILL
        var y13 = dp2px(context,20f).toFloat()/2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2
        canvas?.drawText("I was just beginning to wonder where you were.",
            dp2px(context,98f).toFloat(),dp2px(context,448f).toFloat()+ y13,
            mPaint
        )
        canvas?.drawText("‘Why do you want to know?’   ‘No particular reason. I was just wondering.’",
            dp2px(context,98f).toFloat(),dp2px(context,508f).toFloat()+ y13,
            mPaint
        )
        canvas?.drawText("‘Why do you want to know?’   ‘No particular reason. I was just wondering.’",
            dp2px(context,98f).toFloat(),dp2px(context,568f).toFloat()+ y13,
            mPaint
        )

    }

}